package com.hnevc.myspeaker;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.slidingpanelayout.widget.SlidingPaneLayout;


import com.hnevc.myspeaker.Kuaidi.KuaiDiActivity;
import com.hnevc.myspeaker.Map.MapActivity;
import com.hnevc.myspeaker.Shopping.ProductListActivity;
import com.hnevc.myspeaker.entity.Category;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    SlidingPaneLayout slidingPaneLayout;
    ArrayList<Category> categoryList;
    GridView mGvCategoryView;
    MyAdapter adapter;
    private TextView mItem2;
    private TextView mItem3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
        setToolBar();
        initAdapter();
    }

    private void initView() {
        slidingPaneLayout = findViewById(R.id.slidingPaneLayout);
        toolbar = findViewById(R.id.action_bar);
        mItem2 =  findViewById(R.id.item2);
        mItem3 =  findViewById(R.id.item3);
        mGvCategoryView = findViewById(R.id.gv_category);
        mItem2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MapActivity.class));
            }
        });

        mItem3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, KuaiDiActivity.class));
            }
        });
    }

    private void setToolBar() {
        setSupportActionBar(toolbar);
        View view = LayoutInflater.from(this).inflate(R.layout.toolbar_layout, null);
        ActionBar actionBar = getSupportActionBar();
        ActionBar.LayoutParams params = new ActionBar.LayoutParams(
                ActionBar.LayoutParams.MATCH_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER
        );
        actionBar.setCustomView(view, params);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);


        ImageView imageView = view.findViewById(R.id.menu_main);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (slidingPaneLayout.isOpen()) {
                    slidingPaneLayout.closePane();
                } else {
                    slidingPaneLayout.openPane();
                }
            }
        });
    }
    private void initAdapter() {
        adapter = new MyAdapter();
        mGvCategoryView.setAdapter(adapter);

    }

    private void initData() {
        categoryList = new ArrayList<>();
        Category c = new Category("mobilephone.png","手机");
        categoryList.add(c);
        c = new Category("baowenbei.png","保温杯");
        categoryList.add(c);
        c = new Category("diancilu.png","电磁炉");
        categoryList.add(c);
        c = new Category("diancuifeng.png","电吹风");
        categoryList.add(c);
        c = new Category("erji.png","耳机");
        categoryList.add(c);
        c = new Category("kouhong.png","口红");
        categoryList.add(c);
        c = new Category("luyouqi.png","路由器");
        categoryList.add(c);
        c = new Category("xifashui.png","洗发水");
        categoryList.add(c);
        c = new Category("mobilephone.png","手机");
        categoryList.add(c);
    }


    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return categoryList.size();
        }

        @Override
        public Object getItem(int position) {
            return categoryList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View inflate = View.inflate(MainActivity.this, R.layout.category_gridview_item
                    , null);
            ImageView ivCategoryPic = inflate.findViewById(R.id.iv_category_pic);

            TextView tvCategoryName = inflate.findViewById(R.id.tv_category_name);


            Category categroy = categoryList.get(position);
            ivCategoryPic.setImageResource(getImageIdentify(categroy));
            tvCategoryName.setText(categroy.getCategoryName());

            inflate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(MainActivity.this, ProductListActivity.class));
                }
            });

            return inflate;
        }
    }
    private int getImageIdentify(Category categroy) {
        int identifier = getResources()
                .getIdentifier(categroy.getCategoryPic().split("\\.")[0]
                        , "mipmap", getPackageName());
        return identifier;

    }



}
